package com.jk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jk.dao.TbAdminMapper;
import com.jk.dto.SysMenu;
import com.jk.dto.TbAdmin;
import com.jk.pojo.PagedResponse;
import com.jk.service.TbAdminService;
import com.jk.utils.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author yzf
 * @version 1.0
 * @description: TODO
 * @date 2024/12/9 17:23
 */
@Service
public class TbAdminServiceImpl implements TbAdminService {
    @Autowired
    private TbAdminMapper tbAdminMapper;


    @Override
    public PagedResponse<TbAdmin> findTbAdminList(TbAdmin tbAdmin, Integer pageNum, Integer pageSize) {
        Page<TbAdmin> page = new Page<>(pageNum,pageSize);
        QueryWrapper<TbAdmin> query = new QueryWrapper<>();
        if(null != tbAdmin.getUserName() && !"".equals(tbAdmin.getUserName())) {
            query.like("user_name",tbAdmin.getUserName());
        }
        Page<TbAdmin> list = tbAdminMapper.selectPage(page, query);
        return PaginationUtil.getPagedResponse(list.getRecords(),pageNum,pageSize,list.getTotal());

    }
    @Override
    public void addTbAdmin(TbAdmin tbAdmin) {
        tbAdminMapper.insert(tbAdmin);
    }
    @Override
    public TbAdmin findTbAdminByUserName(String username) {
        QueryWrapper<TbAdmin> query = new QueryWrapper<>();
        query.eq("user_name", username);
        TbAdmin tbAdmin = tbAdminMapper.selectOne(query);
        if (Objects.isNull(tbAdmin)){
            throw new RuntimeException("用户信息不存在");
        }
        List<SysMenu> menus = new ArrayList<>();
        //通过当前用户id获得当前登陆人的菜单集合
        List<SysMenu> menuList = tbAdminMapper.findMenuListByuserId(tbAdmin,0L);
        menuList.stream().forEach(item ->{
            //判断当前节点是否有子节点
            if (isExsistsChildrenNodes(tbAdmin,item)){
                //查询该节点的子节点
                List<SysMenu> menuChildrenNodes = findMenuChildrenNodes(tbAdmin, item);
                item.setChildren(menuChildrenNodes);
            }
            menus.add(item);
        });
        tbAdmin.setMenus(menuList);
        return tbAdmin;
    }
    //查询当前节点下的所有子节点，递归操作
    public List<SysMenu> findMenuChildrenNodes(TbAdmin tbAdmin,SysMenu items){
        List<SysMenu> list = new ArrayList<>();
        List<SysMenu> menuListByuserId = tbAdminMapper.findMenuListByuserId(tbAdmin, items.getMenuId());
        menuListByuserId.stream().forEach(item ->{
            //判断当前节点是否有子节点
            if (isExsistsChildrenNodes(tbAdmin,item)){
                //查询该节点的子节点
                List<SysMenu> menuChildrenNodes = findMenuChildrenNodes(tbAdmin, item);
                item.setChildren(menuChildrenNodes);
            }
            list.add(item);
        });
        return list;
    }
    //判断当前节点是否有子节点
    private boolean isExsistsChildrenNodes(TbAdmin tbAdmin,  SysMenu item) {
        boolean flag = true;
        List<SysMenu> menuListByPrentId = tbAdminMapper.findMenuListByuserId(tbAdmin,item.getMenuId());
        if (CollectionUtils.isEmpty(menuListByPrentId)){
            flag = false;
        }
        return flag;
    }

    @Override
    public void deleteTbAdminById(Integer id) {
        tbAdminMapper.deleteById(id);
    }

    @Override
    public TbAdmin getTbAdminById(Integer id) {
        return tbAdminMapper.selectById(id);
    }

    @Override
    public void upTbAdmin(TbAdmin tbAdmin) {
        tbAdminMapper.updateById(tbAdmin);
    }

    @Override
    public void upstatus(TbAdmin tbAdmin) {
        tbAdminMapper.upstatus(tbAdmin);
    }

    @Override
    public void updatestatus(TbAdmin tbAdmin) {
        tbAdminMapper.updatestatus(tbAdmin);
    }

    @Override
    public List<String> selectPermsByUserId(Integer id) {
        return tbAdminMapper.selectPermsByUserId(id);
    }
}
